﻿function SettingsController($scope, $http) {

    $scope.VoucherApiUrl = "";
    $scope.VoucherList = [];
    $scope.mySelections = [];
    $scope.totalServerItems = 0;
    $scope.CustomerID = "aaaaa";

    $scope.Init = function (voucherApiUrl, customerID) {
        console.log(voucherApiUrl);
        $scope.VoucherApiUrl = voucherApiUrl;
        $scope.CustomerID = customerID;
    };

    angular.element(document).ready(function () {
        $scope.setPagingData(1, 50);
    });

    $scope.filterOptions = {
        filterText: "",
        useExternalFilter: true
    };

    $scope.pagingOptions = {
        pageSizes: [50, 100, 500, 1000],
        pageSize: 50,
        currentPage: 1
    };

    $scope.getPagedDataAsync = function (pageSize, page, searchText) {
        setTimeout(function () {
            $scope.setPagingData(page, pageSize);
        }, 100);
    };

    $scope.gridOptions = {
        data: 'VoucherList',
        columnDefs: [{ field: 'VoucherNo', displayName: 'Voucher No', width: "25%", resizable: false },
        { field: 'Date', displayName: 'Date', width: "25%" },
        { field: 'Type', displayName: 'Type', width: "25%" },
        { field: 'Amount', displayName: 'Amount', width: "25%" }],
        selectedItems: $scope.mySelections,
        multiSelect: false,
        enablePaging: true,
        showFooter: true,
        totalServerItems: 'totalServerItems',
        pagingOptions: $scope.pagingOptions,
        filterOptions: $scope.filterOptions

    };

    $scope.$watch('pagingOptions', function (newVal, oldVal) {
        //if (newVal !== oldVal && newVal.currentPage !== oldVal.currentPage) {
        $scope.getPagedDataAsync($scope.pagingOptions.pageSize, $scope.pagingOptions.currentPage, $scope.filterOptions.filterText);
        //}
    }, true);

    $scope.setPagingData = function (page, pageSize) {
        page = page - 1;
        console.log($scope.VoucherApiUrl);

        $http.get($scope.VoucherApiUrl + "GetVoucherListbyCustomerID/", { params: { "customerID": $scope.CustomerID, "page": page, "pagesize": pageSize } })
        .success(function (data) {

            console.log("success");
            //console.log(data);
            var result = JSON.parse(JSON.parse(data));
            $scope.VoucherList = result.Data;
            $scope.totalServerItems = result.TotalItems;
        }).
        error(function (data, status, headers, config) {
            console.log("error");
        });

    };

}

var app = angular.module('myApp', ['ngGrid']);

